<template>
    <div class="edit-popup" name="cancleleave">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="提前上班">
                    <el-checkbox-group v-model="formData.daylist">
                    <el-checkbox v-for="(item, index) in leaveDays" :key="index" :label="item.id" :disabled="item.iscacle==0 ? true : false ">{{ item.dayName }}</el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="cancleleave">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import useUserStore from '@/stores/modules/user'
import { useDictData } from '@/hooks/useDictOptions'
import { leaveAdd, leaveEdit,leaveCancle } from '@/api/leave'
const userStore = useUserStore()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('mycancle')
const { dictData: optionData } = useDictData('Position,PositionType,holiday')
const popupTitle = computed(() => {
    return mode.value == 'mycancle' ? '销假处理' : '销假审批'
})
const leaveDays = ref([])
const formData = reactive({
    id: '',
    daylist: [] as any
})

const formRules = {
    
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await leaveEdit(formData) : await leaveAdd(formData)
    console.log('提交信息', formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    leaveDays.value = await leaveCancle({
        id: row.id
    })
   // setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
